package org.example.likou150.searchTree;

//  https://leetcode.cn/problems/minimum-absolute-difference-in-bst/?envType=study-plan-v2&envId=top-interview-150
//  核心：中序遍历，记录上一个节点值
public class GetMinimumDifference {
    int min = Integer.MAX_VALUE;
    TreeNode preNode = null;
    public int getMinimumDifference(TreeNode root) {
        if(root==null){
            return Integer.MAX_VALUE;
        }
        getMinimumDifference(root.left);
        if(preNode != null) min = Math.min(min, Math.abs(root.val- preNode.val));
        preNode = root;
        getMinimumDifference(root.right);
        return min;
    }



    private static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
}
